from django import template

register = template.Library()


@register.filter(name='replace')
def replace(value, arg):
    """
    替换字符串中的内容
    使用方式: {{ value|replace:"old:new" }}
    """
    if not isinstance(value, str):
        return value
    if ':' not in arg:
        return value
    old, new = arg.split(':', 1)
    return value.replace(old, new)
